//
//  LoginView.swift
//  weibo1
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

@objc protocol LoginViewDelegite:NSObjectProtocol{
    
    //协议的方法
  optional  func userLogin()
  optional  func userregister()
}


class LoginView: UIView {
    //代理的属性
    weak var LoginDelegite :LoginViewDelegite?
    
    override init(frame: CGRect) {
        
        super.init(frame:frame)
        //添加子控件
        setUpUI()
    }
    //对外的方法
    func setWithInfo(text:String ,imageName:String?){
        //文本
        tipLabel.text = text
        if let name = imageName {
            circleView.image = UIImage(named: name)
            iconView.hidden = true
            bringSubviewToFront(circleView)
            
        }else
            
        {
            startAnimation()
        
        }
        
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 10.0
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        
        //添加动画
        circleView.layer.addAnimation(anim, forKey: nil)

    
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI()
    {
        addSubview(backView)
        
        
        addSubview(circleView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //自动布局
        for i in subviews
        {
            i.translatesAutoresizingMaskIntoConstraints = false
            
        }
        //圆圈
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        //房子
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        //文字
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top , relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        
        
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top , relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //注册
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //背景图片
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["maskView":backView,"regBtn":registerButton]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        //按钮的点击事件
        loginButton.addTarget(self, action: "loginButtonDidClick", forControlEvents: .TouchUpInside)
        
        registerButton.addTarget(self, action:"registerButtonDidClick", forControlEvents: .TouchUpInside)
        
    }
    //实现按钮的点击方法
 @objc private  func loginButtonDidClick()
    {
        //userLogin!必须实现代理方法
        //userLogin? 可选实现的代理方法
        LoginDelegite?.userLogin?()
        
    }
    
 @objc private func registerButtonDidClick()
    {
       LoginDelegite?.userregister?()
        
    }
    //MARK:  懒加载子视图
    //背景图片
    lazy var backView :UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圆圈
    lazy var circleView :UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //房子
    lazy var iconView : UIImageView = UIImageView (image: UIImage(named:"visitordiscover_feed_image_house"))
    //设置label
    lazy var tipLabel : UILabel = {
        let l = UILabel()
        l.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        l.textColor = UIColor.grayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        l.textAlignment = NSTextAlignment.Center
        l.sizeToFit()
        
        return l
        }()
    //登录按钮
    lazy var loginButton :UIButton = {
        let but = UIButton()
        but.setTitle("登录", forState: .Normal)
        but.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        but.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        but.titleLabel?.font = UIFont.systemFontOfSize(15)
        but.sizeToFit()
        return but
        }()
    
    lazy var registerButton :UIButton = {
        let but = UIButton()
        but.setTitle("注册", forState: .Normal)
        but.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        but.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        but.titleLabel?.font = UIFont.systemFontOfSize(14)
        but.sizeToFit()
        return but
        }()
}
